<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>课室物联智能终端</title>
    <link rel="stylesheet" href="lib/layui/css/layui.css"/>
    <link rel="stylesheet" href="css/login.css"/>
</head>

<body style="background: white;">

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login">

    <div class="layadmin-user-login-main" style="display: none;">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <span style="line-height: 30px;margin-left: 8px;font-family: '楷体';font-size: 2rem;">智能控制终端系统</span>
        </div>
        <form class="layadmin-user-login-box layadmin-user-login-body layui-form" lay-filter="login">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                       for="LAY-user-login-username"></label>
                <input type="text" name="account" id="LAY-user-login-username" lay-verify="required" placeholder="用户名"
                       class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                       for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required"
                       placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                       for="LAY-user-login-password"></label>
                <input type="text" name="securityCode" id="securityCode" lay-verify="required"
                       placeholder="验证码" class="layui-input" style="width: 200px;display: inline-block;">
                <img style="width: 120px;height: 36px;margin-left: 8px;margin-top: -2px;" id="captcha" onclick="reloadCaptcha()"/>
            </div>

            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="LAY-user-login-submit">登 入</button>
            </div>

        </form>
    </div>

</div>
<div style="width:100%;line-height:60px;position:absolute;bottom:10px;">
    <div style="text-align:center;">网络中控控制软件&nbsp;&nbsp;&nbsp;&nbsp;v3.5</div>
</div>
<script type="text/javascript" src="lib/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-1.12.1.min.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript" src="js/sha.js"></script>
<script>
    var layer;
    var mCapCode=null;
    var captaTimer=null
    layui.use('layer', function () {
        layer = layui.layer;
        var index = layer.open({
            type: 3,
            icon: 2,
            content: '<div style="padding-top:40px;min-width:80px;margin-left:-22px;text-align:center;">校验中...</div>',
            shade: 0.1,
            offset: [($(window).height()) / 2 - 80, ($(window).width()) / 2 - 10]

        });
        checkLogin(function (inTag) {
            if (inTag) {
                layer.close(index);
                window.location.href = 'main.html';
            } else {
                setTimeout(function () {
                    layer.close(index);
                    $('.layadmin-user-login-main').show();
                }, 1000)

            }
        })
    });

    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(LAY-user-login-submit)', function (data) {
            login(data.field);
            return false;
        });
    });

    refreshCaptcha();
    captaTimer=setInterval(function(){
        refreshCaptcha()
    },30000)

    function refreshCaptcha(){
        mCapCode=(parseInt(100000000*Math.random())).toString(16)
         $.ajax({
            type: "get",
            url: "/sys/captcha/gen?code="+mCapCode,
            async: true,
            success: function (res) {
                $("#captcha").attr("src",res)
            }
        });

    }

    function reloadCaptcha(){
       if(captaTimer){
       clearInterval(captaTimer);
       }
        refreshCaptcha()
        captaTimer=setInterval(function(){
        refreshCaptcha()
    },30000)
    }

    function checkLogin(callback) {
        var session = $.cookie('user_session');
        var user_id = $.cookie('user_id');
        if (user_id && session && session.length > 0) {
            checkSession(user_id,session, callback);
        } else {
            callback(false);
        }
    }

    function checkSession(user_id,session, callback) {
        var time=(new Date()).getTime();
        var nonce=Math.random().toString(36).substr(2, 15);
        var url="/app/admin/session/check";
        var args={
            user_id:user_id,
            session:session,
            url:url,
            nonce:nonce,
            time:time
        }
        var keys = Object.keys(args)
        keys = keys.sort()
        var newArgs = {}
        keys.forEach(function (key) {
            newArgs[key.toLowerCase()] = args[key]
        })

        var string = ''
        for (var k in newArgs) {
            if (newArgs[k] !== undefined) {
                string += '&' + k + '=' + newArgs[k]
            }
        }
        string = string.substr(1)
        var signature = hex_sha1(string)
        $.ajax({
            type: "post",
            url: url,
            headers: {
                user_id: user_id,
                session: session,
                time:time,
                nonce:nonce,
                sign:signature
            },
            async: true,
            success: function (res) {
                if (res.code == 0) {
                    $.cookie('user_session', session, {expires: 1});
                    callback(true)
                } else {
                    callback(false)
                }
            },
            error:function(){
                callback(false)
            }
        });
    }

    function login(field) {
        var url="/sys/login"
        field.password =  Math.random().toString(36).substr(2, 6)+hex_sha1(field.password)+Math.random().toString(36).substr(2, 6)
        field.code=mCapCode
        $.ajax({
            type: "POST",
            data:  JSON.stringify(field),
            contentType:"application/json",
            dataType:"json",
            url: url,
            success: function (res) {
                if (res.code == 0) {
                    var data = res.data;
                    var session = data.session;
                    var user_id = data.user_id;
                    $.cookie('user_session', session, {expires: 1});
                    $.cookie('user_id', user_id, {expires: 1});
                    window.localStorage.setItem('user_name', data.user_name);
                   window.location.href = 'main.html';
                } else {
                    reloadCaptcha()
                    layer.msg(res.msg, {icon: 5,time: 5000});
                }
            }
        });
    }
</script>
</body>

</html>